<template>
  <div class="app-container flex-container">
    <div class="left">
      <!-- <Tree @change="deptChange"></Tree> -->
      <!-- <el-input v-model="req._mobile_" type="text" placeholder="搜索班组" @change="getClassList"></el-input> -->
      <div class="list-group">
        <div class="item" :class="{active:active && item.id==active.id}" v-for="(item,index) in groupList" :key="index" @click="handleClickItem(item)">{{item.name}}</div>
      </div>
    </div>
    <div class="center" style="flex: 1;overflow: auto;">
      <div class="content">
        <el-row :gutter="20" style="margin-bottom: 20px;margin-right: 0;">
          <el-col :span="6"><el-input v-model="req._memberCode_" type="text" placeholder="请输入工号" @change="getUserList"></el-input></el-col>
          <el-col :span="6"><el-input v-model="req._memberName_" type="text" placeholder="请输入姓名" @change="getUserList"></el-input></el-col>
          <el-col :span="6">
            <el-button type="primary" icon="el-icon-search" @click="getUserList">查询</el-button>
            <el-button type="success" icon="el-icon-plus" @click="add">新增</el-button>
          </el-col>
        </el-row>
        <el-table :data="userList" border @row-click="rowClick">
          <el-table-column type="index" label="序号" width="50" align="center" show-overflow-tooltip>
            <template slot-scope="{$index}">
              {{$index + 1 + (req.page - 1) * req.pageSize}}
            </template>
          </el-table-column>
          <el-table-column prop="memberName" label="姓名" align="left" show-overflow-tooltip></el-table-column>
          <!-- <el-table-column prop="userName" label="账号" min-width="120" align="left" show-overflow-tooltip></el-table-column> -->
          <el-table-column prop="memberCode" label="工号" min-width="100" align="left" show-overflow-tooltip></el-table-column>
          <el-table-column prop="groupName" label="部门" min-width="120" align="left" show-overflow-tooltip></el-table-column>
          <el-table-column prop="cardNum" label="工牌" min-width="120" align="left" show-overflow-tooltip></el-table-column>
          <!-- <el-table-column prop="workTeamName" label="班组" min-width="120" align="left" show-overflow-tooltip></el-table-column>
          <el-table-column prop="workTime" label="考勤时间" min-width="120" align="left" show-overflow-tooltip></el-table-column> -->
          <!-- <el-table-column prop="status" label="状态" min-width="80" align="left" show-overflow-tooltip>
            <template slot-scope="{ row }">
              <span>{{ row.status == 1 ? '在职' :row.status == 0 ? '离职' : '其他' }}</span>
            </template>
          </el-table-column> -->
          <el-table-column label="操作" width="150" align="center">
            <template slot-scope="{ row }">
              <el-button @click="editCard(row, $event)" type="text" size="small">工牌</el-button>
              <el-button @click="editArea(row, $event)" type="text" size="small">考勤点</el-button>
              <el-button @click="remove(row, $event)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <Pagination v-show="total" :total="total" :page.sync="req.page" :limit.sync="req.pageSize" @pagination="getUserList"></Pagination>
      </div>
    </div>
    <div class="" style="height: 100%;flex: 0 0 25vw;">
      <Map ref="map" style="width: 100%;height: 100%;"></Map>
    </div>

    <!-- 弹窗 -->
    <roadEdit :show.sync="showRoadEdit" :notReq="true" :item="currentUser"></roadEdit>
    <cardEdit :show.sync="showCardEdit" :item="currentUser" @change="getUserList"></cardEdit>
    <editCheck :show.sync="showCheckEdit" :item="currentUser" @change="getUserList"></editCheck>

    <addUser ref="UserRef" :show.sync="showAddUser" :clazzGroup="active" @change="getUserList"></addUser>
  </div>
</template>

<script>
import Map from "@/components/Map";
import roadEdit from "../attendRoadLine/components/edit.vue";
import editCheck from "./components/editCheck.vue";
import cardEdit from "./components/cardEdit.vue";
import addUser from "./components/addUser.vue";

import { getPersonList, del, getCheckArea } from "@/api/person";
import { classList } from "@/api/classGroup";
export default {
  name: "Account",
  components: {
    roadEdit,
    cardEdit,
    Map,
    editCheck,
    addUser,
  },
  data() {
    return {
      showRoadEdit: false,
      showCardEdit: false,
      currentUser: {},
      userList: [],
      groupList: [], // 班组
      active: null,

      total: 0,
      req: {
        groupPath: null,
        _name_: null,
        _mobile_: null,
        orderBy: "id desc",
        status: null, //1启用 0禁用
        page: 1,
        pageSize: 10,
      },

      visiblePass: false,
      showCheckEdit: false,
      group: null,
      showAddUser: false,
    };
  },
  created() {
    this.getClassList();
    this.getUserList();
  },
  methods: {
    // 部门选中发生改变
    deptChange(data, node) {
      if (this.req.groupPath == data.groupPath) return;
      this.req.page = 1;
      this.req.groupPath = data.groupPath;
      this.group = structuredClone(data);
      this.getUserList();
    },
    async getClassList() {
      try {
        const res = await classList({ page: 1, pageSize: 999 });
        this.groupList = res.data;
      } catch (error) {}
    },
    handleClickItem(item) {
      this.active = item;
      this.req.teamId = item.id;
      this.getUserList();
    },
    // 获取部门下的用户
    getUserList() {
      getPersonList(this.req).then((res) => {
        this.userList = res.data || [];
        this.total = res.total;
      });
    },
    // 点击工牌
    editCard(row, event) {
      event.stopPropagation();
      this.currentUser = JSON.parse(JSON.stringify(row));
      this.showCardEdit = true;
    },
    // 修改考勤点
    editArea(row, event) {
      event.stopPropagation();
      this.currentUser = JSON.parse(JSON.stringify(row));
      this.showCheckEdit = true;
    },
    async rowClick(row, column, event) {
      event.stopPropagation();
      if (!this.$refs.map || !this.$refs.map.map) return;
      this.$refs.map.remove();
      try {
        const res = await getCheckArea({ memberId: row.memberId });
        if (res.data && res.data.length) {
          this.$refs.map.createLayers(res.data);
        }
      } catch (error) {}
    },
    add() {
      if (!this.active) return this.$message.error("请先选择班组！");
      this.showAddUser = true;
    },
    remove(row, event) {
      event.stopPropagation();
      this.$modal
        .confirm("是否确认删除此账号？")
        .then((res) => {
          return del({ id: row.id });
        })
        .then((res) => {
          this.$modal.msgSuccess("操作成功！");
          this.getList();
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;

  .left {
    width: 300px;
    margin-right: 20px;
    overflow: auto;

    .custom-tree-node {
      display: flex;
      align-items: center;
    }
  }

  .right {
    flex: 1;
    overflow: auto;
  }
}
</style>
<style lang="scss">
.app-container {
  .left {
    .list-group {
      height: 100%;
      background: #fff;
      padding: 10px 20px;
      box-sizing: border-box;
      border-radius: 10px;

      .item {
        padding: 5px 10px;
        cursor: pointer;
        border-radius: 5px;
        &:hover{
            color: #fff;
            background: rgb(21, 119, 247);
        }
      }
      .active {
        color: #fff;
        background: rgb(21, 119, 247);
      }
      .item + .item {
        margin-top: 10px;
      }
    }
    .el-button--mini.is-circle {
      padding: 3px;
      margin-left: 3px;
    }

    .el-tree--highlight-current
      .el-tree-node.is-current
      > .el-tree-node__content {
      background-color: rgb(83, 143, 252);
      color: #fff;
      border-radius: 3px;
    }
  }
}
</style>
